package com.dds.demo.util;

import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentCacheHashMap {
	private static ConcurrentHashMap<String, Object> cacheMap = new ConcurrentHashMap<>();

	/**
	 * 获取缓存的对象
	 * 
	 * @param key
	 * @return
	 */
	public static Object getCache(String key) {

		key = getCacheKey(key);
		if (cacheMap.containsKey(key)) {
			return cacheMap.get(key);
		}
		return null;
	}

	/**
	 * 初始化缓存
	 * 
	 * @param key
	 */
	public static void setCache(String key, Object ob) {
		cacheMap.put(key, ob);
	}

	/**
	 * 拼接一个缓存key
	 * 
	 * @param key
	 * @return
	 */
	private static String getCacheKey(String key) {
		return Thread.currentThread().getId() + "-" + key;
	}

	/**
	 * 移除缓存信息
	 * 
	 * @param key
	 */
	public static void removeCache(String key) {
		cacheMap.remove(getCacheKey(key));
	}

}
